package com.swiot.biz.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.swiot.biz.constants.TemplateConstants;
import com.swiot.biz.domain.Rule;
import com.swiot.biz.mapper.RuleMapper;
import com.swiot.biz.service.IRuleService;
import com.swiot.common.core.redis.RedisCache;
import com.swiot.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 规则业务Service业务层处理
 *
 * @author kingdom
 * @date 2025-08-24
 */
@Service
public class RuleServiceImpl implements IRuleService {
    @Autowired
    private RuleMapper ruleMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询规则业务
     *
     * @param id 规则业务主键
     * @return 规则业务
     */
    @Override
    public Rule selectRuleById(String id) {
        return ruleMapper.selectRuleById(id);
    }

    /**
     * 查询规则业务列表
     *
     * @param rule 规则业务
     * @return 规则业务
     */
    @Override
    public List<Rule> selectRuleList(Rule rule) {
        return ruleMapper.selectRuleList(rule);
    }

    /**
     * 查询规则业务列表
     *
     * @return 规则业务列表
     */
    @Override
    public List<Rule> queryRuleList() {
        return null;
    }

    public JSONObject getRuleCache() {
        String result = redisCache.getCacheObject(TemplateConstants.AlarmRules.RULE);
        if (StringUtils.isBlank(result)) {
            result = generateRuleCache();
        }
        return JSONObject.parseObject(result);
    }

    public String generateRuleCache() {
        Rule rule = new Rule();
        rule.setRuleStatus(TemplateConstants.RuleStatus.ENABLE.longValue());
        List<Rule> rules = ruleMapper.selectRuleList(rule);
        JSONObject result = new JSONObject();
        for (Rule rule1 : rules) {
            JSONObject ruleObject = new JSONObject();
            ruleObject.put(TemplateConstants.AlarmRules.RULE_FUNC, rule1.getRuleCode());
            ruleObject.put(TemplateConstants.AlarmRules.RULE_PARAM, rule1.getRuleParams());
            result.put(rule1.getRuleName(), ruleObject);
        }
        String value = result.toJSONString();
        redisCache.setCacheObject(TemplateConstants.AlarmRules.RULE, value, 1 * 1000 * 1, TimeUnit.SECONDS);
        return value;
    }

    /**
     * 新增规则业务
     *
     * @param rule 规则业务
     * @return 结果
     */
    @Override
    public int insertRule(Rule rule) {
        int result = ruleMapper.insertRule(rule);
        generateRuleCache();
        return result;
    }

    /**
     * 修改规则业务
     *
     * @param rule 规则业务
     * @return 结果
     */
    @Override
    public int updateRule(Rule rule) {
        int result = ruleMapper.updateRule(rule);
        generateRuleCache();
        return result;
    }

    /**
     * 批量删除规则业务
     *
     * @param ids 需要删除的规则业务主键
     * @return 结果
     */
    @Override
    public int deleteRuleByIds(String[] ids) {
        int result = ruleMapper.deleteRuleByIds(ids);
        generateRuleCache();
        return result;
    }

    /**
     * 删除规则业务信息
     *
     * @param id 规则业务主键
     * @return 结果
     */
    @Override
    public int deleteRuleById(String id) {
        int result = ruleMapper.deleteRuleById(id);
        generateRuleCache();
        return result;
    }

    /**
     * 检查规则名称是否存在
     *
     * @param ruleName 规则名称
     * @return 是否存在
     */
    @Override
    public boolean checkRuleNameExists(String ruleName) {
        Rule rule = ruleMapper.selectRuleByRuleName(ruleName);
        return rule != null;
    }

    /**
     * 检查规则名称是否存在（排除指定ID）
     *
     * @param ruleName 规则名称
     * @param id       规则ID
     * @return 是否存在
     */
    @Override
    public boolean checkRuleNameExistsExcludeId(String ruleName, String id) {
        Rule rule = ruleMapper.selectRuleByRuleNameExcludeId(ruleName, id);
        return rule != null;
    }
}
